<?php
namespace app\controller\teacher;

use app\BaseController;
use app\model\Chapter;
use app\model\Course;
use app\model\Discuss;
use app\model\StudentCourse;
use app\model\VStudentCourseCount;
use app\model\Exam;
use think\Request;
use \think\facade\Db;
use app\utils\Excel;

class TeacherCourse extends BaseController
{
    /**
     * 列出所在课程
     */
    public function listMyCourse()
    {
        $course = Course::where('TID',$this->user['TID'])->order('createTime','DESC')->select();
        return resultSuccess($course);
    }

    /**
     * 查找课程名
     */
    public function getCourseName(){
        $courseID = $this->request->param('courseID');
        $course = Course::where(['TID'=>$this->user['TID'],'courseID'=>$courseID])->find()->toArray();
        $courseStudentNumber = StudentCourse::where('courseID',$courseID)->count();
        $course['studentNumber'] = $courseStudentNumber;
        if($courseID == null){
            return resultError('无此课程');
        }
        return resultSuccess($course);
    }

    /**
     * 查看课程学生
     */
    public function listCourseStudent(){
        $courseID = $this->request->param('courseID');
        // $pageSize = $this->request->param('pageSize');
        $course = Course::where(['TID'=>$this->user['TID'],'courseID'=>$courseID])->find();
        if($course == null){
            return resultError('权限验证失败');
        }
        $examSum = Exam::where('courseID',$courseID)->count('credit');
        $discussCount = Discuss::where('courseID',$courseID)->count('credit');
        $mediaCount = Chapter::where('courseID',$courseID)->join('media','media.chapterID = chapter.chapterID')->count('credit');
        $courseCount = VStudentCourseCount::where(['courseID'=>$courseID])->order('totalCredit','desc')->select();
        return resultSuccess([
            'list'=>$courseCount,
            'examSum'=>$examSum,
            'discussSum' =>$discussCount,
            'mediaSum' => $mediaCount,
            'course' => $course
        ]);
    }

    /**
     * 查看课程统计
     */
    public function countCourse(){
        $courseID = $this->request->param('courseID');
        // $pageSize = $this->request->param('pageSize');
        $course = Course::where(['TID'=>$this->user['TID'],'courseID'=>$courseID])->find();
        if($course == null){
            return resultError('权限验证失败');
        }
        $courseCount = VStudentCourseCount::where(['courseID'=>$courseID])->order('totalCredit','desc')->select()->toArray();
        $header = ["A1"=>"NO","B1" => "学号", "C1" => "姓名", "D1" => "班级名","E1"=>"	多媒体积分","F1"=>"测验积分","G1"=>"讨论积分","H1"=>"总积分"];
        $type= true;
        // 设置下载文件保存的名称
        $fileName = time();
        // 调用方法导出excel
        Excel::export($header,$type,$courseCount,$fileName);
    }

}

